/***********************************************************************
Compile       : gcc -O3 -o simple simple.c design.c
Usage         : ./simple mode1 mode2 N L inputfile 
                (./simple 0 0 11 12 Budding.txt)
mode1         : 0 normal ; 1 cycled ; 2 triggled cycled
mode2         : 0 disallow self-activation ; 1 allow self_activation
N             : number of the nodes
L             : number of the steps        
Output        : n2 min_link designability1 designability2
n2            : number of 1 in the process (exclude the last fix point)
min_link      : minimum links of the qualified network
designability1: the minimum designability for one node
designability2: total designability
***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "design.h"

int main (int argc, char *argv[])
{
	int N, L, i, j, k, l, n1, n2;
	int mode1, mode2, flag;
	float ratio;
	FILE *fp;
	char buf[256];

	mode1 = atoi (argv[1]);
	mode2 = atoi (argv[2]);
	N = atoi (argv[3]);
	L = atoi (argv[4]);

	int pathway[L][N];
	struct designability designability;

	designability.NS2 = calloc (sizeof (int), N);
	designability.min_link = calloc (sizeof (int), N);

	fp = fopen (argv[5], "r");

	while (fgets (buf, 256, fp))
	{
		n1 = n2 = 0;
		for (i = 0; i < L; i++)
		{
			for (j = 0; j < N; j++)
			{
				fscanf (fp, "%d", &pathway[i][j]);
				n1 += pathway[i][j];
				if (i <= L - 3 || (i == L - 2 && mode1 != 2))
					n2 += pathway[i][j];
			}
			fgets (buf, 256, fp);
		}

		if (mode1 == 2)
			n2 --;

		/* Get the number of solution using enumerating algorithm */
		flag = calc_design (N, L, pathway, mode1, mode2, 0, 0, 0, 0, 0, 0, &designability, NULL, NULL, NULL);
		if (flag == 1)
			printf ("c %3d %3d %5d %lg", n2, designability.min_links, designability.NS2_min, designability.NS);
		else
			printf ("c %3d %3d %5d %lg", n2, 0, 0, 0.0);

		flag = calc_design (N, L, pathway, mode1, mode2, 1, 0, 0, 0, 0, 0, &designability, NULL, NULL, NULL);
		if (flag == 1)
			printf (" %3d %5d %lg", designability.min_links, designability.NS2_min, designability.NS);
		else
			printf (" %3d %5d %lg", 0, 0, 0.0);

		flag = calc_design (N, L, pathway, mode1, mode2, 2, 0, 0, 0, 0, 0, &designability, NULL, NULL, NULL);
		if (flag == 1)
			printf (" %3d %5d %lg", designability.min_links, designability.NS2_min, designability.NS);
		else
			printf (" %3d %5d %lg", 0, 0, 0.0);

		flag = calc_design (N, L, pathway, mode1, mode2, 3, 0, 0, 0, 0, 0, &designability, NULL, NULL, NULL);
		if (flag == 1)
			printf (" %3d %5d %lg\n", designability.min_links, designability.NS2_min, designability.NS);
		else
			printf (" %3d %5d %lg\n", 0, 0, 0.0);

		for (i = 0; i < L; i++)
		{
			for (j = 0; j < N; j++)
			{
				printf ("%d ", pathway[i][j]);
			}
			printf ("\n");
		}
	}

	fclose (fp);
}
